<cfcomponent output="false">
	
	<!--- --->
		
	<cffunction name="sendEmail" access="public" output="true" returntype="string">
		<cfargument name="subject" required="true" />
		<cfargument name="text" required="true" />
		<cfargument name="html" required="false" default="" />
		<cfargument name="to" required="true" />
		<cfargument name="from" required="false" default="noreply@craftivity.me" />
		<cfargument name="spoolable" required="false" default="true" />
		<cfargument name="userID" required="false" default="" />
		<cfargument name="template_name" required="false" default="" />
		
		<cfset var local = {} />
		
		<cftry>
			<cfmail to="#arguments.to#" from="#arguments.from#" subject="#arguments.subject#" type="text" spoolenable="#arguments.spoolable#">
				<cfif arguments.text neq ''>
					<cfset arguments.text = rereplace(arguments.text,"\t","","ALL") />
					<cfmailpart charset="utf-8" type="text/plain">#arguments.text#</cfmailpart>
				</cfif>
				<cfif arguments.html neq ''>
					<cfmailpart charset="utf-8" type="text/html">
						#arguments.html#
					</cfmailpart>
				</cfif>
			</cfmail>
			
			<!--- log the email if userID is passed in --->
			<cfif arguments.userID neq "" and arguments.template_name neq "">
				<cfset local.id = $.database.save(
					table="user_email_log",
					id="",
					user_id=arguments.userID,
					template_name=arguments.template_name,
					subject=arguments.subject,
					body=arguments.text,
					email=arguments.to
				) />
			</cfif>
			
			<cfcatch>
				<cfdump var="#cfcatch#"><cfabort>

				<cfreturn false />
			</cfcatch>
		</cftry>
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendCraftivityBetaEmail" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		<cfargument name="userID" required="true" />
		<cfargument name="first_name" required="true" />
		
		<cfset var local = {} />
		
		<cfsavecontent variable="local.message">
			<div><h2>Welcome to Craftivity</h2></div>
			<div><cfif arguments.first_name neq "">Hello #arguments.first_name#, </cfif>We're so excited to welcome you to Craftivity!</div>
			
			<div>&nbsp;</div>
			
			<div>Start using Craftivity now by clicking on the link below.</div>

			<div><a href="#$.site.getVar("rootURL")#/beta_signup.cfm?activate=#arguments.userID#">Activate Craftivity</a></div>
			<div>&nbsp;</div>
			<div>You can also copy the url into your browser.</div>
			<div><a href="#$.site.getVar("rootURL")#/beta_signup.cfm?activate=#arguments.userID#">#$.site.getVar("rootURL")#/beta_signup.cfm?activate=#arguments.userID#</a></div>
		</cfsavecontent>
		
		<cfsavecontent variable="local.textonly">Welcome to Craftivity!
			<cfif arguments.first_name neq "">Hello #arguments.first_name#, </cfif>We're so excited to welcome you to Craftivity!
			
			Start using Craftivity now by clicking on the link below
			#$.site.getVar("rootURL")#/beta_signup.cfm?activate=#arguments.userID#
			
			You can also copy the link into your browser.
		</cfsavecontent>
		
		<cfset $.run.it('emailService.sendEmail',{
			text=local.textonly,
			html=local.message,
			subject="Activate your Craftivity Beta Account",
			to=arguments.email,
			from="Activation @ Craftivity <activation@craftivity.me>"
		}) />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendRecoveryEmail" access="public" output="true" returntype="void">
		<cfargument name="email" required="true" />
		<cfargument name="userID" required="true" />
		<cfargument name="first_name" required="true" />
		
		<cfset var local = {} />
		
		<cfsavecontent variable="local.message">
			<div><h2><cfif arguments.first_name neq "">Hi #arguments.first_name#, </cfif>Forgot your password?</h2></div>
			<div>That's OK we won't judge.</div>
			
			<div>&nbsp;</div>
			
			<div>Just click on this link and you can decide on a new one.</div>
			
			<div>#$.site.getVar("rootURL")#/reset_password.cfm?activate=#right(arguments.userID,12)##left(arguments.userID,20)#</div>
			</br>
			<div>You can also copy the url into your browser.</div>
		</cfsavecontent>
		
		<cfsavecontent variable="local.textonly"><cfif arguments.first_name neq "">Hi #arguments.first_name#, </cfif>Forgot your password?
			That's OK we won't judge.
			
			Just click on this link and you can decide on a new one.
			#$.site.getVar("rootURL")#/reset_password.cfm?activate=#right(arguments.userID,12)##left(arguments.userID,20)#
			
			(you can also copy this link into your browser)
		</cfsavecontent>
		
		<cfset $.run.it('emailService.sendEmail',{
			text=local.textonly,
			html=local.message,
			subject="Make a brand new Craftivity password.",
			to=arguments.email,
			from="iforgot@craftivity.me"
		}) />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendOpenBetaSpotsEmail" access="public" output="true" returntype="string">
		<cfargument name="email" required="true" />

		<cfset var local = {} />
		
		<cfsavecontent variable="local.message">
			<div>Hey there. We're opening up more spots for <b>Craftivity: the goal setting & productivity tool for creative people.</b></div>
			<div>&nbsp;</div>
			<div>There are only 50 spots available, and I'm thinking they'll be filled within a few hours.</div>
			<div>&nbsp;</div>
			<div>Click here to Sign up</div>
			<div><a href="#$.site.getVar("rootURL")#/beta_signup.cfm?email=#arguments.email#">Goto Craftivity</a></div>
			<div>&nbsp;</div>
			<div>You can also copy the url into your browser.</div>
			<div><a href="#$.site.getVar("rootURL")#/beta_signup.cfm?email=#arguments.email#">#$.site.getVar("rootURL")#/beta_signup.cfm?email=#arguments.email#</a></div>
			<div>&nbsp;</div>
			<div>Claim your account and make this your most productive year ever!</div>
			<div>&nbsp;</div>
			<div>- Peter</div>
			<div>&nbsp;</div>
			<div>Not interested? Click below to be removed from this list.</div>
			<div><a href="#$.site.getVar("rootURL")#/beta_signup.cfm?unsubscribe=#arguments.email#">unsubscribe</a></div>
		</cfsavecontent>
		
		<cfsavecontent variable="local.textonly">Hey there. We've just opened up more spots for Craftivity: the goal setting & productivity tool for creative people.
			There are only 50 spots available, and I'm expecting them to be filled quickly.
			
			Click below to Sign up
			#$.site.getVar("rootURL")#/beta_signup.cfm?email=#arguments.email#
			
			You can also copy the url into your browser.
			
			Claim your account and make this your most productive year ever!
			
			- Peter
			
			Not interested? Click on the link below to be removed from this list.
			#$.site.getVar("rootURL")#/beta_signup.cfm?unsubscribe=#arguments.email#
		</cfsavecontent>
				
		<cfset local.result = $.run.it('emailService.sendEmail',{
			text=trim(local.textonly),
			html=local.message,
			subject="Hurry! Craftivity Beta spots are available again.",
			to=arguments.email,
			from="Peter @ Craftivity <Peter@craftivity.me>",
			spoolable=false
		}) />
		
		<cfreturn local.result />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendInactivityEmail" access="public" output="true" returntype="string">
		<cfargument name="userID" required="true" />
		<cfargument name="email" required="true" />
		<cfargument name="first_name" required="false" default="" />
		<cfargument name="template_name" required="false" default="Inactive" />
		<cfargument name="goal_name" required="false" default="" />
		<cfargument name="important_name" required="false" default="" />

		<cfset var local = {} />
		
		<cfsavecontent variable="local.message">
			<div>Hey <cfif arguments.first_name neq"">#arguments.first_name#<cfelse>stranger</cfif>,</div>
			<div>We've noticed that you haven't visited Craftivity in a while.</div>
			<div>&nbsp;</div>
			<cfif arguments.goal_name neq "">
				<div>Remember that goal of yours,</div>
				<div><b>#rereplace(arguments.goal_name,'\.','','ALL')#</b>?</div>
				<div>&nbsp;</div>
				<div>How is that coming along? Craftivity can help you stay on track and make it happen.</div>
			<cfelseif arguments.important_name neq "">
				<div>Did you ever complete that task,</div>
				<div><b>#rereplace(arguments.important_name,'\.','','ALL')#</b>?</div>
				<div>Were you able to get around to it? Craftivity can help you stay on top of these important weekly tasks.</div>
			<cfelse>
				<div>Have you thought of what goals you'd like to reach this year?</div>
				<div>Perhaps you want to set up your new business,</div>
				<div>or double your revenue from last year.</div>
				<div>Maybe you want to increase your social media following.</div>
				<div>&nbsp;</div>
				<div>Whatever your goals are, Craftivity is here to help you get there.</div>
			</cfif>
			<!--- <div>Craftivity is constantly improving and we think you should </div> --->
			<div>&nbsp;</div>
			<div>Click on the link below to sign in, and make this year your most productive yet.</div>
			<div><a href="#$.site.getVar("rootURL")#/login.cfm?email=#arguments.email#">Sign in to Craftivity</a></div>
			<div>&nbsp;</div>
			<div>Or copy the url into your browser.</div>
			<div><a href="#$.site.getVar("rootURL")#/login.cfm?email=#arguments.email#">#$.site.getVar("rootURL")#/login.cfm?email=#arguments.email#</a></div>
			<div>&nbsp;</div>
			<div>We'd love to see you achieve all your goals this year.</div>
			<div>&nbsp;</div>
			<div>- Peter</div>
			<div>&nbsp;</div>
			<div>Is Craftivity just not right for you? Respond to this email and let us know why, or how we can improve.</div>
			<div>&nbsp;</div>
			<div><a href="#$.site.getVar("rootURL")#/unsubscribe.cfm?email=#arguments.email#">unsubscribe</a></div>
		</cfsavecontent>
		
		<cfsavecontent variable="local.textonly">Hey <cfif arguments.first_name neq"">#arguments.first_name#<cfelse>stranger</cfif>,

			We've noticed that you haven't visited Craftivity in a while.
			<cfif arguments.goal_name neq "">
				Remember that goal of yours,
				"#rereplace(arguments.goal_name,'\.','','ALL')#"?
				How is that coming along? Craftivity can help you stay on track and make it happen.
			<cfelseif arguments.important_name neq "">
				Did you ever complete that task,
				"#rereplace(arguments.important_name,'\.','','ALL')#"?
				Were you able to get around to it? Craftivity can help you stay on top of these important weekly tasks.
			<cfelse>Have you thought of what goals you'd like to reach this year?
				
				Perhaps you want to set up your new business,
				or double your revenue from last year.
				Maybe you want to increase your social media following.

				Whatever your goals are, Craftivity is here to help you get there.
			</cfif>

			Click on the link below to sign in, and make this year your most productive yet.
			#$.site.getVar("rootURL")#/login.cfm?email=#arguments.email#

			Or copy and past the link into your browser.

			We'd love to see you achieve all your goals this year.

			- Peter

			Is Craftivity just not right for you? Respond to this email and let us know why, or how we can improve.
			
			Click on the link below to unsubscribe
			#$.site.getVar("rootURL")#/unsubscribe.cfm?email=#arguments.email#
		</cfsavecontent>
		
		<cfset local.result = $.run.it('emailService.sendEmail',{
			text=local.textonly,
			html=local.message,
			subject="Where have you been?",
			to=arguments.email,
			from="Peter @ Craftivity <Peter@craftivity.me>",
			spoolable=false,
			userID=arguments.userID,
			template_name=arguments.template_name
		}) />
		
		<cfreturn local.result />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="cron_sendInactiveEmails" access="public" output="true" returntype="string">
	
		<cfset var local = {} />
		
		<cfset local.inactives = $.run.it('craftivityService.getUserData',{
			email_template_name="inactive",
			last_request=dateTimeFormat(dateAdd('ww',-1,now()),'mm/dd/yyyy HH:nn:ss'),
			limit=15,
			unsubscribe=0
		}) />
		
		<cfloop query="local.inactives">
			<!--- get the most recent incomplete goal --->
			<cfif local.inactives.goal_count gt 0>
				<cfquery name="local.goals" datasource="#$.site.getVar('datasource')#">
					select goal.name
					from goal
					inner join user on user.id = goal.user_id
						and user.id = #$.database.uuid(local.inactives.id)#
					where goal.active = 1
					and goal.created >= date_add(user.created,INTERVAL 10 SECOND) 
					order by goal.created desc
					limit 1
				</cfquery>
			<cfelse>
				<cfset local.goals = {name=""} />
			</cfif>
			
			<!--- get the most recent important --->
			<cfif local.inactives.important_count gt 0>
				<cfquery name="local.important" datasource="#$.site.getVar('datasource')#">
					select important.name
					from important
					inner join user on user.id = important.user_id
						and user.id = #$.database.uuid(local.inactives.id)#
					where important.active = 1
					and important.created >= date_add(user.created,INTERVAL 10 SECOND) 
					order by important.created desc
					limit 1
				</cfquery>
			<cfelse>
				<cfset local.important = {name=""} />
			</cfif>
			
			<!--- get the most recent incomplete important --->
			<cfset $.run.it('emailService.sendInactivityEmail',{
				userID=local.inactives.id,
				email=local.inactives.email,
				first_name=local.inactives.first_name,
				template_name="inactive",
				goal_name=local.goals.name,
				important_name=local.important.name
			}) />
		</cfloop>
	
		<cfreturn local.inactives.recordcount />
				
	</cffunction>

	<!--- --->

</cfcomponent>			